using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using Player;
using DG.Tweening;
using Unity.VisualScripting;

namespace Manager
{
    public class GameManager : MonoBehaviour
    {

        public static GameManager Instance;
        public GameObject level;//关卡
        [SerializeField]
        private GameObject[] levelChilds;//一个关卡的每一波
        public int enemysNum = 0;//每一波的敌人数量
        [SerializeField]
        [Tooltip("当前是第几关")]
        private int index = 0;//当前是第几波
        private GameObject BottomIconPrefab;

        void Awake()
        {
            if(Instance != null)
            {
                Destroy(gameObject);
            }    
            else
                Instance = this;

            Debug.Log("GameManager Awake");
        }
        // Start is called before the first frame update
        void Start()
        {
#if UNITY_ANDROID
            //Application.targetFrameRate = 60; // 60fps
#endif
            BottomIconPrefab = Resources.Load<GameObject>("Prefabs/Enemy/UI/BottomIcon");

            if(level != null)
            {
                levelChilds = new GameObject[level.transform.childCount];   
                for(int i = 0; i < levelChilds.Length; i++)
                {
                    levelChilds[i] = level.transform.GetChild(i).gameObject;
                    levelChilds[i].SetActive(false);
                }
                /*                levelChilds[index].SetActive(true);
                                enemysNum = levelChilds[index].transform.childCount;*/

                StartCoroutine(StartNextLevel());
            }
        }

        private void Update()
        {

        }

        //每一波中敌人数量的变化->是否到下一波次
        public event Action OnEnemyNumChanged;
        //每一波敌人数量减少到0的事件=>经验值会追踪到玩家身上 && 玩家经验值增加并且更新UI
        public event Action OnEnemyNumIsZero;

        private void OnEnable()
        {
            OnEnemyNumChanged += EnemyNumChangedHandler;
        }
        private void OnDisable()
        {
            OnEnemyNumChanged -= EnemyNumChangedHandler;
        }

        /// <summary>
        /// 下一个波次的事件处理器
        /// </summary>
        public void EnemyNumChangedHandler()
        {
            enemysNum--;
            if (enemysNum <= 0)
            {
                index++;
                if (index >= levelChilds.Length)
                {
                    Debug.Log("第一关结束");
                    index = 0;
                    LoadNextScene();
                }
                OnEnemyNumIsZero ?.Invoke();
/*                enemysNum = levelChilds[index].transform.childCount;
                levelChilds[index].SetActive(true);*/

                StartCoroutine(StartNextLevel());
            }
        }

        /// <summary>
        /// 开启下一个波次的协程
        /// </summary>
        /// <returns></returns>
        IEnumerator StartNextLevel()
        {
            //等一帧，让经验物品移动到玩家身上
            yield return null;
            enemysNum = levelChilds[index].transform.childCount;

            //等待一点时间在开启下一个波次
            yield return new WaitForSeconds(2f);

            //获取levelChilds[index]下面的子物体,并创建一个底部图标
            for (int i = 0; i < levelChilds[index].transform.childCount; i++)
            {
                GameObject enemy = levelChilds[index].transform.GetChild(i).gameObject;
                Vector3 pos = new Vector3(enemy.transform.position.x, 10.1f, enemy.transform.position.z);
                //Instantiate(BottomIconPrefab, pos, Quaternion.identity);
                //利用对象池
                CreateBottomIcon(pos);
            }

            yield return new WaitForSeconds(1.5f);//这个时间和底部图标的销毁时间要一致

            levelChilds[index].SetActive(true);


        }

        /// <summary>
        /// 创建底部图标
        /// </summary>
        /// <param name="pos"></param>
        private void CreateBottomIcon(Vector3 pos)
        {
            GameObject icon = PoolManager.Release(BottomIconPrefab, pos, Quaternion.identity);

        }
        /// <summary>
        /// 事件响应
        /// </summary>
        public void EnemyNumChanged()
        {
            OnEnemyNumChanged?.Invoke();
        }



        //加载下一个场景
        public static void LoadNextScene()
        {
            int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
            SceneManager.LoadScene(currentSceneIndex + 1);
        }
    }
}
